package org.csu.lestore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.lestore.api.common.CommonResponse;
import org.csu.lestore.api.entity.OrderItem;
import org.csu.lestore.api.entity.Orders;
import org.csu.lestore.api.persistence.OrderItemMapper;
import org.csu.lestore.api.persistence.OrdersMapper;
import org.csu.lestore.api.service.OrderService;
import org.csu.lestore.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrderItemMapper orderItemMapper;

    @Override
    public CommonResponse<Orders> insertOrder(Orders order) {
        Calendar calendar =Calendar.getInstance();
        Date date = calendar.getTime();
        order.setOrderDate(date);
        ordersMapper.insert(order);
        return CommonResponse.createForSuccess("新建订单成功",order);
    }

    @Override
    public CommonResponse<OrderItem> insertOrderItem(OrderItem orderItem) {
        QueryWrapper<Orders> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_id",orderItem.getOrderId());
        Orders orders = ordersMapper.selectOne(orderQueryWrapper);
        if(orders!=null){
            orderItemMapper.insert(orderItem);
            return CommonResponse.createForSuccess("插入订单条目成功",orderItem);
        }else{
            return CommonResponse.createForError("订单不存在");
        }

    }

    @Override
    public CommonResponse<Orders> deleteOrder(int orderId) {
        deleteOrderItemByOrderId(orderId);
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",orderId);
        ordersMapper.delete(ordersQueryWrapper);
        deleteOrderItemByOrderId(orderId);
        return CommonResponse.createForSuccessMessage("删除订单成功");
    }

    @Override
    public CommonResponse<OrderItem> deleteOrderItemByOrderId(int orderId) {
        QueryWrapper<OrderItem> orderItemQueryWrapper = new QueryWrapper<>();
        orderItemQueryWrapper.eq("order_id",orderId);
        orderItemMapper.delete(orderItemQueryWrapper);
        return CommonResponse.createForSuccessMessage("删除成功");
    }

    @Override
    public CommonResponse<OrderVO> getOrder(int orderId) {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",orderId);
        Orders orders = ordersMapper.selectOne(ordersQueryWrapper);
        OrderVO orderVO = orderToOrderVO(orders);
        return CommonResponse.createForSuccess(orderVO);
    }

    @Override
    public CommonResponse<Orders> setOrderState(int orderId, char orderState) {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",orderId);
        Orders orders = ordersMapper.selectOne(ordersQueryWrapper);
        if(orders!=null){
            orders.setOrderState(orderState);
            ordersMapper.update(orders,ordersQueryWrapper);
            return CommonResponse.createForSuccessMessage("修改成功");
        }else{
            return CommonResponse.createForError("订单不存在");
        }
    }

    @Override
    public CommonResponse<Orders> updateOrder(Orders orders) {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("order_id",orders.getOrderId());
        Orders existOrders = ordersMapper.selectOne(ordersQueryWrapper);
        if(existOrders!=null){
            ordersMapper.update(orders,ordersQueryWrapper);
            return CommonResponse.createForSuccessMessage("修改成功");
        }else{
            return CommonResponse.createForError("订单不存在");
        }
    }

    @Override
    public CommonResponse<IPage<OrderVO>> getOrderListInPage(int curPage, int size) {
        IPage<Orders> ordersPage = new Page<>(curPage,size);
        ordersMapper.selectPage(ordersPage,null);

        List<Orders> ordersList = ordersPage.getRecords();
        List<OrderVO> orderVOList = new ArrayList<>();
        IPage<OrderVO> orderVOPage = new Page<>(curPage,size);

        if(ordersList!=null){
            for(Orders orders : ordersList){
                OrderVO orderVO = orderToOrderVO(orders);
                orderVOList.add(orderVO);
            }
        }
        orderVOPage.setRecords(orderVOList);
        orderVOPage.setPages(ordersPage.getPages());
        orderVOPage.setTotal(ordersPage.getTotal());

        return CommonResponse.createForSuccess(orderVOPage);
    }

    @Override
    public CommonResponse<IPage<OrderVO>> getOrderListInPage(int curPage, int size,int userId) {
        IPage<Orders> ordersPage = new Page<>(curPage,size);
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("user_id",userId);
        ordersMapper.selectPage(ordersPage,ordersQueryWrapper);

        List<Orders> ordersList = ordersPage.getRecords();
        List<OrderVO> orderVOList = new ArrayList<>();
        IPage<OrderVO> orderVOPage = new Page<>(curPage,size);

        if(ordersList!=null){
            for(Orders orders : ordersList){
                OrderVO orderVO = orderToOrderVO(orders);
                orderVOList.add(orderVO);
            }
        }
        orderVOPage.setRecords(orderVOList);
        orderVOPage.setPages(ordersPage.getPages());
        orderVOPage.setTotal(ordersPage.getTotal());

        return CommonResponse.createForSuccess(orderVOPage);
    }

    @Override
    public CommonResponse<IPage<OrderItem>> getOrderItemListInPage(int curPage, int size,int orderId) {
        IPage<OrderItem> orderItemPage = new Page<>(curPage,size);
        QueryWrapper<OrderItem> orderItemQueryWrapper = new QueryWrapper<>();
        orderItemQueryWrapper.eq("order_id",orderId);
        orderItemMapper.selectPage(orderItemPage ,orderItemQueryWrapper);
        return CommonResponse.createForSuccess(orderItemPage);
    }

    private OrderVO orderToOrderVO(Orders order){
        OrderVO orderVO = new OrderVO();

        orderVO.setOrderId(order.getOrderId());
        orderVO.setUserId(order.getUserId());
        orderVO.setTotalPrice(order.getTotalPrice());
        orderVO.setOrderState(order.getOrderState());
        orderVO.setRemarks(order.getRemarks());
        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setSendAddrId(order.getSendAddrId());
        orderVO.setRevAddrId(order.getRevAddrId());

        QueryWrapper<OrderItem> orderItemQueryWrapper = new QueryWrapper<>();
        orderItemQueryWrapper.eq("order_id",order.getOrderId());
        List<OrderItem> orderItemList = orderItemMapper.selectList(orderItemQueryWrapper);

        orderVO.setOrderItemList(orderItemList);

        return orderVO;
    }
}
